package com.lee.generic;

import java.util.ArrayList;
import java.util.List;

public class Pair<T> {


    private T first;
    private T second;

    public Pair() {
        this.first = null;
        this.second = null;
    }

    public Pair(T first, T second) {
        this.first = first;
        this.second = second;
    }

    public T getFirst() {
        return first;
    }

    public void setFirst(T first) {
        this.first = first;
    }

    public T getSecond() {
        return second;
    }

    public void setSecond(T second) {
        this.second = second;
    }
}


class ArrayAlg {
    public static Pair<String> minmax(String[] a) {


        if (a == null || a.length == 0) return null;

        String min = a[0];
        String max = a[0];
        for (int i = 0; i < a.length; i++) {
            if (min.compareTo(a[i]) > 0) min = a[i];
            if (max.compareTo(a[i]) < 0) max = a[i];
        }

        return new Pair<>(min, max);
    }


    public static <T> T getMiddle(T... args) {
        return args[args.length / 2];
    }

    public static <T extends Comparable> T min(T... a) {
        if (a == null || a.length == 0) return null;
        T smallest = a[0];
        for (int i = 0; i < a.length; i++) {
            if (smallest.compareTo(a[i]) > 0) smallest = a[i];
        }
        return smallest;
    }
}

class Test {
    public static void main(String[] args) {
        String[] words = {"a", "b", "c"};
        /*first and last*/
        Pair<String> minmax = ArrayAlg.minmax(words);
        System.out.println(minmax.getFirst());
        System.out.println(minmax.getSecond());
        /*middle*/
        String middle = ArrayAlg.getMiddle(words);
        System.out.println(middle);
        /*min*/
        String min = ArrayAlg.min(words);
        System.out.println(min);
        /*max*/



    }
}
